package com.example.smartagrisys.controller;

import com.example.smartagrisys.common.Result;
import com.example.smartagrisys.entity.Transaction;
import com.example.smartagrisys.service.TransactionService;
import com.example.smartagrisys.util.SecurityUtil;
import com.example.smartagrisys.vo.TransactionStatsVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 交易记录控制器
 */
@Slf4j
@RestController
@RequestMapping("/transaction")
public class TransactionController {

    @Autowired
    private TransactionService transactionService;

    private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    /**
     * 获取当前用户的交易记录
     *
     * @return 交易记录列表
     */
    @GetMapping("/my")
    public Result<List<Transaction>> getMyTransactions(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate) {
        Long userId = SecurityUtil.getCurrentUserId();
        if (userId == null) {
            return Result.error("用户未登录");
        }

        log.info("获取用户[{}]交易记录，日期范围: {} - {}", userId, startDate, endDate);
        
        List<Transaction> transactions;
        if (startDate != null && endDate != null) {
            transactions = transactionService.getTransactionsByDateRange(userId, startDate, endDate);
        } else {
            transactions = transactionService.getUserTransactions(userId);
        }
        
        return Result.success(transactions);
    }

    /**
     * 获取交易统计
     *
     * @return 统计信息
     */
    @GetMapping("/stats")
    public Result<TransactionStatsVO> getTransactionStats() {
        Long userId = SecurityUtil.getCurrentUserId();
        if (userId == null) {
            return Result.error("用户未登录");
        }

        log.info("获取用户[{}]交易统计", userId);
        
        TransactionStatsVO statsVO = transactionService.getTransactionStatsByUserId(userId);
        
        return Result.success(statsVO);
    }

    /**
     * 创建交易记录
     *
     * @param transaction 交易记录信息
     * @return 创建结果
     */
    @PostMapping
    public Result<Boolean> createTransaction(@RequestBody Transaction transaction) {
        Long userId = SecurityUtil.getCurrentUserId();
        if (userId == null) {
            return Result.error("用户未登录");
        }

        log.info("创建交易记录，用户ID: {}, 类型: {}, 金额: {}", userId, transaction.getType(), transaction.getAmount());
        
        // 设置用户ID
        transaction.setUserId(userId);
        
        // 如果没有设置交易日期，则使用当前时间
        if (transaction.getTransactionDate() == null) {
            transaction.setTransactionDate(LocalDateTime.now());
        }

        // 设置创建时间和更新时间
        transaction.setCreateTime(LocalDateTime.now());
        transaction.setUpdateTime(LocalDateTime.now());
        
        boolean result = transactionService.createTransaction(transaction);
        if (!result) {
            return Result.error("创建交易记录失败");
        }
        
        return Result.success(true);
    }
} 